###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/Menu.pm.pl -> Menu module                               #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::Menu;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";
require "Authenticate.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = ("input" => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my @Menu = (
    { name => "Settings", title => "Settings", icons => [
      { icon => "Menu/SystemOptions.gif", caption => "System Options", action => "SystemOptions", permission => "SYSTEMOPTIONS" },
      { icon => "Menu/GeneralOptions.gif", caption => "General Options", action => "GeneralOptions", permission => "GENERALOPTIONS" },
      { icon => "Menu/ExtraFields.gif", caption => "Extra Resource Fields", action => "ExtraFields", permission => "EXTRAFIELDS_VIEW" }
    ]},
    { name => "Database", title => "Database Management", icons => [
      { icon => "Menu/Categories.gif", caption => "Categories", action => "Categories", permission => "CATEGORIES_VIEW" },
      { icon => "Menu/Resources.gif", caption => "Resources", action => "Resources", permission => "RESOURCES_VIEW" },
      { icon => "Menu/Admins.gif", caption => "Admins", action => "Admins", permission => "ADMINS_VIEW" },
      { icon => "Menu/Levels.gif", caption => "Levels", action => "Levels", permission => "LEVELS_VIEW" },
    ]},
    { name => "Skin", title => "Skin Management", icons => [
      { icon => "Menu/Skins.gif", caption => "Skins", action => "Skins", permission => "SKINS_VIEW" },
      { icon => "Menu/StyleSheets.gif", caption => "StyleSheets", action => "StyleSheets", permission => "STYLESHEETS_VIEW" },
      { icon => "Menu/TemplateSets.gif", caption => "Template Sets", action => "TemplateSets", permission => "TEMPLATESETS_VIEW" },
      { icon => "Menu/LanguageSets.gif", caption => "Language Sets", action => "LanguageSets", permission => "LANGUAGESETS_VIEW" }
    ]},
    { name => "Utilities", title => "Utilities", icons => [
      { icon => "Menu/Backups.gif", caption => "Backups", action => "Backups", permission => "BACKUPS_VIEW" },
      { icon => "Menu/ConvertDatabase.gif", caption => "Convert Database", action => "ConvertDatabase", permission => "CONVERTDATABASE" },
      { icon => "Menu/ImportExport.gif", caption => "Import/Export", action => "ImportExport", permission => "IMPORTEXPORT" },
      { icon => "Menu/Email.gif", caption => "Email List Members", action => "Email", permission => "EMAIL" },
      { icon => "Menu/Expire.gif", caption => "Expire All Static Pages", action => "Expire", permission => "EXPIRE" },
      { icon => "Menu/InstallPackage.gif", caption => "Install Package", action => "InstallPackage", permission => "INSTALLPACKAGE" },
      { icon => "Menu/Update.gif", caption => "Update SuperLinks", action => "Update", permission => "UPDATE" }
    ]}
  );

  if (@SL::CPMENU && scalar(@SL::CPMENU) >= 1) {
    push(@Menu, { name => "Plugins", title => "Plugins", icons => \@SL::CPMENU });
  }

  my @Sections = $SL::CGI->param('Sections');
  
  my %Sections;
  foreach my $section (@Sections) {
    $Sections{ $section } = 1;
  }
  
  my $Page = $Dialog->MenuBody(body => "<center><b>SuperLinks<br>[$SL::VERSION]</b></center>");
     $Page = $Dialog->MenuBox(body => $Page);
  
  my $Body;
  foreach my $section (@Menu) {
    my @icons;
    foreach my $icon (@{ $section->{'icons'} }) {
      next if ($icon->{'permission'} && !&Authenticate::CheckPermission(ACTION => $icon->{'permission'}, RETURN => 1));
      $icon->{'url'}    ||= "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=$icon->{'action'}&SID=$SL::ADMIN{'SID'}";
      $icon->{'target'} ||= "CPBODY";
      push(@icons, $icon);
    }
    
    next unless (scalar(@icons) >= 1);

    if ($Sections{ $section->{'name'} }) {
      $Body .= $Dialog->MenuHeader(type => "active", title => $section->{'title'}, url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Menu&".&Standard::MapArray("Sections=\${}", [(grep { $_ ne $section->{'name'} } @Sections)], "&")."&SID=$SL::ADMIN{'SID'}");

      my $Icons = $Dialog->MenuIcons(icons => \@icons);
      $Body .= $Dialog->MenuBody(body => $Icons);
    } else {
      $Body .= $Dialog->MenuHeader(type => "inactive", title => $section->{'title'}, url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Menu&".&Standard::MapArray("Sections=\${}", \@Sections, "&")."&Sections=$section->{'name'}&SID=$SL::ADMIN{'SID'}");
    }
  }

  $Page .= $Dialog->MenuBox(body => $Body);

  my $Logo = <<HTML;
<img border="0" src="$SYSTEM->{'PUBLIC_URL'}/Images/logo.gif" width="160" height="220" usemap="#logo">
<map name="logo">
<area href="$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Logout&SID=$SL::ADMIN{'SID'}" target="_top" alt="Logout" shape="rect" coords="129, 146, 153, 169">
<area href="$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Summary&SID=$SL::ADMIN{'SID'}" target="CPBODY" alt="Summary" shape="rect" coords="7, 147, 31, 170">
</map>
HTML

  $Page  = $Dialog->Menu(icon => $Logo, body => $Page);
  $Page  = $Dialog->Page(body => $Page, body_extra => qq~class="menu"~);

  return $Page;
}

1;